/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedm@list.ru
 */

#include <QtCore/QCoreApplication>
#include <qdebug.h>
#include "mcore.h"
#include "def.h"
#include "windows.h"
#include "ntservice.h" // functions of windows nt service

int tbn_main(int argc, char ** argv){
    QCoreApplication a(argc, argv);
    mcore core(a.applicationDirPath());
    core.start();
    return a.exec();
}

// start entry
int main(int argc, char *argv[])
{
    bool servicemode=false; // on or off service mode. see -u --usermode
    // command line parameters
    for(int i=0;i<=argc;i++){
        if(QString(argv[i])=="-h" or
           QString(argv[i])=="--help" or
           QString(argv[i])=="-?"){
            printf(t_help);
            exit(0);
        }
        if(QString(argv[i])=="-v" or
           QString(argv[i])=="--version"){
            printf("%s version %s.%s build %s for %s\nAuthors:\n%s\n"
                   ,d_appname,d_version_major,d_version_minor,d_version_build,d_os,d_authors);
            exit(0);
        }
        if(QString(argv[i])=="-i" or
           QString(argv[i])=="-install"){
            if(i+1<=argc){
            QString pa(argv[i+1]);
                if(!instserv(pa)){
                    printf("err inst serv");
                    exit(1);
                }else{
                    printf("ok inst serv");
                    exit(0);
                }
            }else{
                 printf("err inst serv - failed install service parameter");
                 exit(2);
            }

        }
        if(QString(argv[i])=="-d" or
           QString(argv[i])=="-delete"){
            if(!delserv()){
                printf("err del serv");
                exit(1);
            }else{
                printf("ok del serv");
                exit(0);
            }

        }
        if(QString(argv[i])=="-s" or
           QString(argv[i])=="-service"){
            servicemode=true;
        }
}
    if(!servicemode){
        //CLI mode
        return tbn_main(argc,argv);
    }else{
        //Service NT
        SERVICE_TABLE_ENTRY DispatcherTable[] =
            { { SomeServiceName,
             (LPSERVICE_MAIN_FUNCTION)ServiceMain },
          { NULL, NULL }
        };
        //
        if(!StartServiceCtrlDispatcher(DispatcherTable)) {
            printf("StartServiceCtrlDispatcher: Error \n");
            return 1;
        }
    }
}
